<?php if(isset($error_template) && !empty($error_template)){ ?>
<fieldset id="errortemplateinfo" class="m-4">
    <h2 class="text-danger text-primary"><strong>自定义设置的前台模板错误,已经启用默认模板:<?php echo $error_template;?></strong></h2>
    <h3 class="text-danger text-secondary"><strong>The front template of the custom setting is wrong, the default template is already enabled:<?php echo $error_template;?></strong></h3>
</fieldset>
<script type="text/javascript">//<![CDATA[
    $(document).ready(function(){
        var bodyelem = $('body');
        $("#errortemplateinfo").detach().prependTo(bodyelem);
    });//]]>
</script>
<?php }
if(GetPostFlagEnable() && !empty($PostFlagsMarkData)) { ?>
<div class="modal fade" id="flagReason" tabindex="-1" role="dialog" aria-labelledby="flagReasonLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="flagReasonLabel" data-deftxt="输入标记原因">输入标记原因</h4>
            </div>
            <div class="modal-body">
                <textarea class="form-control" data-deftxt="标记原因或者说明,选填" placeholder="标记原因或者说明,选填" name="flagReasonText" id="flagReasonText"></textarea>
                <em>回帖内容:</em><p id="postContentView"></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="flagReasonRun" onclick="RunFlag(this,'flagReasonText')">提交标记</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">//<![CDATA[
    var IsDebug = false;//设置是否处于调试环境
    var FirstPostClassname = "<?php echo $first['classname'];?>";
    <?php if($reviseerr_delay && ($allowupdate || $first['allowupdate'])) { ?>
    $(document).ready(function() {
            var thread_update_link = $('.card-thread .media-body A.post_update',document).addClass('reviseerr');
            var reviseerr_tooltip = thread_update_link.tooltip({
                title:'<?php echo $reviseerr_delay_tip;?>',
                placement:'top'
            });
            reviseerr_tooltip.tooltip('show');
    });
   <?php } ?>
if(typeof jform != 'undefined' && jform.length == 1){
    jform.off('submit');/*强制重新注册事件*/
    jform.on('submit', function() {
        jform.reset();
        jsubmit.button('loading');
        var postdata = jform.serialize();
        $.xpost(jform.attr('action'), postdata, function(code, message) {
            if(code == 0) {
                var s = '<ul>'+message+'</ul>';
                var jli = $(s).find('li.post');//ghx修正此处
                jli.insertBefore($('.postlist > .post').last());
                jsubmit.button('reset');
                $('#message').val('');

                // 楼层 +1
                var jfloor = $('#newfloor');
                jfloor.html(xn.intval(jfloor.html()) + 1);

                // 回复数 +1
                var jposts = $('.posts');
                jposts.html(xn.intval(jposts.html()) + 1);

            } else if(code < 0) {
                $.alert(message);
                jsubmit.button('reset');
            } else {
                jform.find('[name="'+code+'"]').alert(message).focus();
                jsubmit.button('reset');
            }
        });
        return false;
    });
}

    $(document).ready(function () {
        //主题帖的标记classname处理:
        if(FirstPostClassname == 'post' ) FirstPostClassname = '';
        else{
            if(FirstPostClassname.startsWith('post ')) FirstPostClassname = FirstPostClassname.substr(5);
        }
        if(FirstPostClassname.length > 0){
            $('.card-thread .card-body > .message').addClass(FirstPostClassname);
        }
        //生成回帖标记按钮:
        if (typeof(postlistcount) != "undefined" && postlistcount > 0 && typeof(generate_postflag_manage_hander) != "undefined" && $.isFunction(generate_postflag_manage_hander) && <?php echo empty($postlist)?'false':'true'; ?>) {//生成管理标签的按钮:
            generate_postflag_manage_hander();
        }
        if (typeof(generate_firstpostflag_manage_btn) != "undefined" && $.isFunction(generate_firstpostflag_manage_btn) && <?php echo $allowupdate?'true':'false'; ?>) {//生成主题贴管理标签的按钮:
            generate_firstpostflag_manage_btn();
        }
        //生成标记操作对话框:
        $('#flagReason').modal({keyboard: true, show: false});
        //从后台获取标记详情
        $('.postlist .post .media-body .message,.card-thread .message').delegate('>UL.ghx_stamp > LI',"click",{},function(){
            if(typeof($(this).data('title')) != 'undefined'){
                if($(this).data('title').length > 0){
                    $(this).tooltip('show');
                }
            }
            else{
                var isFirst = false;
                var FirstPostElem = null;
                var FirstPostElemP = $(this).parents('.card-thread');
                if(FirstPostElemP.length > 0){
                    FirstPostElem = $('.media #thread_marker',FirstPostElemP);
                }
                if(FirstPostElem != null && FirstPostElem.length > 0) isFirst = true;
                var pid = 0;
                if( ! isFirst) pid = $(this).parents('.postlist > .post').data('pid');
                else pid = FirstPostElem.data('pid');
                var postdata = new Object();
                var urlAttach = '';
                if(!!IsDebug) urlAttach = "?debug=1&XDEBUG_SESSION_START=PHPSTORM";
                var posturl = 'ghxpost-getinfo-' + pid + '-' + $(this).data('flag') + urlAttach;
                $.post(posturl, postdata, function(data,status,xhr) {
                    if (status == "success"){
                        if (typeof (data) == 'string') {
                            if (data[0] == '{' && data[data.length - 1] == '}') {
                                data = JSON.parse(data);
                            }
                        }
                        if (typeof (data) == 'object' && data.code == 0){
                            if (typeof data.sucopt == 'object') {
                                if (typeof data.sucopt.getinfo == 'object' && count(data.sucopt.getinfo) > 0) {
                                    $.each(data.sucopt.getinfo, function (index, value){
                                        var postobj = null;
                                        if( ! isFirst) postobj = $('.postlist .post[data-pid=\'' + index + '\']');
                                        else  postobj = $('.card-thread .message');
                                        if (postobj.length > 0){
                                            var stampobj = $('UL.ghx_stamp', postobj);
                                            if (stampobj.length > 0){
                                                $.each(value, function (zFlag, zInfo){
                                                    if(zFlag != 'id' && zFlag != 'pid') {
                                                        var stampitem = $("LI[data-flag='" + zFlag + "']", stampobj);
                                                        if (stampitem.length > 0) {
                                                            stampitem.data('title', zInfo);//存储
                                                            var old_title = stampitem.attr('title');
                                                            stampitem.attr('title',zInfo);
                                                            stampitem.tooltip('show');//立即显示
                                                            stampitem.attr('title',old_title);
                                                        }
                                                    }
                                                });
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    }
                });
            }

        });
        //显示标记详情
        $('.postlist .post .media-body .message,.card-thread .message').delegate('>UL.ghx_stamp > LI',"mouseenter",{},function(){
            var isFirst = false;
            var FirstPostElem = null;
            var FirstPostElemP = $(this).parents('.card-thread');
            if(FirstPostElemP.length > 0){
                FirstPostElem = $('.media #thread_marker',FirstPostElemP);
            }
            if(FirstPostElem != null && FirstPostElem.length > 0) isFirst = true;
            var delbtn = $('.postflag_delete',this);
            if(delbtn.length == 0){
                var pid = 0;
                var optPFCfg = CurPostFlagList;
                if( ! isFirst) pid = $(this).parents('.postlist > .post').data('pid');
                else {
                    pid = FirstPostElem.data('pid');
                    optPFCfg = PostFlagList;
                }
                if(pid > 0){
                    var curflag = $(this).data('flag');
                    var deltitle = '删除';
                    if(typeof(optPFCfg[curflag]) != 'undefined'){
                        deltitle += '['+optPFCfg[curflag]['title']+']标记';
                    }
                    var urlAttach = '';
                    if(!!IsDebug) urlAttach = "?debug=1&XDEBUG_SESSION_START=PHPSTORM";
                    var curUrl = 'ghxpost-clear-'+pid+'-'+curflag+urlAttach;
                    var delobj = $('<a data-url="'+curUrl+'" href="javascript:void(0);" class="text-grey postflag_delete _confirm"><i class="icon-remove" title="'+deltitle+'"></i><span class="d-none">删除</span></a>');
                    $(this).append(delobj);
                    delbtn = delobj;
                    delbtn.bind('click',{url:curUrl,sender:delbtn},function(e){
                        e.stopPropagation();
                        clearpostflag(e.data.url,e.data.sender);
                    });
                }
            }
            if(delbtn.length == 1){
                delbtn.css('display','inline');
            }

        });
        $('.postlist .post .media-body .message,.card-thread .message').delegate('>UL.ghx_stamp > LI',"mouseleave",{},function(){
            var delbtn = $('.postflag_delete',this);
            if(delbtn.length == 1){
                delbtn.css('display','none');
            }
        });


    });

    /**
     * 显示设置帖子的标记的操作对话框,等待用户确定
     * @param evt 事件触发元素对象
     * @param flag 标记值
     * @constructor
     */
    function SetPostFlag(evt, flag) {
        if (evt && !!flag && $('#flagReason').length > 0) {
            var btn = $('button.postflagmanagebtn', $(evt).parent().parent().parent());
            if (btn.length > 0) {//先进行参数验证
                var tid = parseInt(btn.data("tid"));
                var pid = parseInt(btn.data("pid"));
                var IsFirstPost = false;
                if('first' == btn.data("type")) IsFirstPost = true;
                if (tid > 0 && pid > 0) {
                    var frbtn = $('#flagReasonRun');
                    if (frbtn.length > 0) {
                        if(!IsFirstPost)
                            $('#postContentView').text($('.message', $(evt).parents('.media-body')).first().text());
                        else
                            $('#postContentView').text('');
                        var ReasonLabelCtl = $("#flagReasonLabel");
                        var ReasonTextCtl = $("#flagReasonText");
                        frbtn.data('tid', tid).data('pid', pid).data('flag', flag).data('type', (IsFirstPost ? 1 : 0));
                        if(!IsFirstPost) $('#flagReason').detach().appendTo($(evt).parents('.postlist .post'));
                        else $('#flagReason').detach().appendTo($('.message',$(evt).parents('.card-thread')));
                        /*todo:此处根据标记的具体情况修改:*/
                        var tmpvar = null;
                        switch (flag) {
                            case 'striphtml':
                                ReasonLabelCtl.text("文本化帖子内容");
                                ReasonTextCtl.prop('placeholder',"要保留的HTML标签,例:<br><hr>,不填为默认<br>表示只保留换行");
                                break;
                            case 'clear':
                                ReasonLabelCtl.text("清除帖子标记");
                                ReasonTextCtl.prop('placeholder',"要删除的标记列表,多个以逗号分隔,all或者不填表示全部");
                                break;
                            default:
                                ReasonLabelCtl.text("标记为[" + $(evt).text() + "]");
                                tmpvar = ReasonTextCtl.data('deftxt');
                                if(ReasonTextCtl.prop('placeholder') != tmpvar)
                                ReasonTextCtl.prop('placeholder',tmpvar);
                                break;
                        }
                        $('#flagReason').modal('show');
                    }
                }
            }
        }
    }

    /**
     * 对后台GhxPost操作返回的数据进行前台处理
     * @param data {array} 返回的数据
     * @param IsFirstPost {boolean} 是否为主帖
     * @param PostFlagCfg {array} 标记配置表
     * @constructor
     */
    function CatchFlagPostData(data,IsFirstPost,PostFlagCfg){
        if (typeof(data) == 'object' && typeof(data.sucopt) == 'object' && typeof(PostFlagCfg) != 'undefined') {
            var postobj = null;
            var cur_pid = parseInt(data.pid);
            if(!IsFirstPost) postobj = $('.postlist .post[data-pid=\'' + cur_pid + '\']');
            else postobj = $('.card-thread .message');

            if (typeof data.sucopt.add == 'object' && count(data.sucopt.add) > 0) {
                $.each(data.sucopt.add, function (index, value) {
                    if (postobj.length > 0) {
                        var stampobj = $('UL.ghx_stamp', postobj);
                        if (stampobj.length == 0) {
                            var stampobjbox = null;
                            if(!IsFirstPost) stampobjbox = $('.media-body .message', postobj);
                            else stampobjbox = postobj;
                            if (stampobjbox.length > 0) {
                                stampobjbox.prepend($('<ul class="post_icons ghx_stamp"></ul>'));
                                stampobj = $('UL.ghx_stamp', postobj);
                            }
                        }
                        if (stampobj.length > 0) {
                            $.each(value, function (xFlag, xInfo) {
                                var additionClass = 'ghx-post-' + xFlag;
                                var flagName = xFlag;
                                if (typeof (PostFlagCfg[xFlag]['AdditionClass']) != 'undefined')
                                    additionClass = PostFlagCfg[xFlag]['AdditionClass'];
                                if (typeof (PostFlagCfg[xFlag]['title']) != 'undefined')
                                    flagName = PostFlagCfg[xFlag]['title'];
                                if (additionClass.length > 0) postobj.addClass(additionClass);
                                var additionClass2 = 'ghx_stamp_' + xFlag;
                                if (!xInfo) xInfo = (typeof(PostFlagCfg[xFlag]['title']) == 'string' ?PostFlagCfg[xFlag]['title'] : flagName);
                                var flagElem = $('<li class="' + additionClass2 + '" data-flag="' + xFlag + '" title="' + flagName + '">&nbsp;&nbsp;</li>');
                                flagElem.data('title',xInfo);
                                stampobj.prepend(flagElem);
                                var old_title = flagElem.attr('title');
                                flagElem.attr('title',xInfo);
                                flagElem.tooltip('show');//立即显示
                                flagElem.attr('title',old_title);
                            });
                        }
                    }
                });
            }

            if (typeof data.sucopt.del == 'object' && count(data.sucopt.del) > 0) {
                $.each(data.sucopt.del, function (index, value) {
                    if (postobj.length > 0) {
                        var stampobj = $('UL.ghx_stamp', postobj);
                        if (stampobj.length > 0) {
                            $.each(value, function (xFlag, xInfo) {
                                var additionClass = 'ghx-post-' + xFlag;
                                var flagName = xFlag;
                                if (typeof (PostFlagCfg[xFlag]['AdditionClass']) != 'undefined')
                                    additionClass = PostFlagCfg[xFlag]['AdditionClass'];
                                if (additionClass.length > 0) postobj.removeClass(additionClass);
                                var flagElem = $('LI.ghx_stamp_'+xFlag,stampobj);
                                if(flagElem.length > 0){
                                    flagElem.remove();
                                }
                            });
                        }
                    }
                });
            }

            if (typeof data.sucopt.upd == 'object' && count(data.sucopt.upd) > 0) {
                $.each(data.sucopt.upd, function (index, value) {
                    if (postobj.length > 0) {
                        var stampobj = $('UL.ghx_stamp', postobj);
                        if (stampobj.length > 0) {
                            $.each(value, function (xFlag, xInfo) {
                                var additionClass = 'ghx_stamp_' + xFlag;
                                var flagName = xFlag;
                                var flagElem = $('LI.'+additionClass,stampobj);
                                if(flagElem.length > 0){
                                    flagElem.tooltip('destroy');//重新生成
                                    var old_title = flagElem.attr('title');
                                    flagElem.data('title',xInfo);
                                    flagElem.attr('title',xInfo);
                                    flagElem.tooltip('show');
                                    flagElem.attr('title',old_title);
                                }
                            });
                        }
                    }
                });
            }

            if (typeof data.sucopt.message_fmt == 'object' && count(data.sucopt.message_fmt) > 0) {
                $.each(data.sucopt.message_fmt, function (index, value) {
                    var postmsgobj = null;
                    var fieldsetObj = null;
                    var flagReasonObj = null;
                    if(!IsFirstPost) postmsgobj = $('.postlist .post[data-pid=\'' + index + '\'] .media-body .message');
                    else {
                        postmsgobj = postobj;
                        fieldsetObj = $('FIELDSET.fieldset').detach();
                        flagReasonObj = $('#flagReason').detach();
                    }
                    if (postmsgobj.length > 0) {
                        var flags = $('UL.ghx_stamp',postmsgobj);
                        if(flags.length > 0)
                            flags.detach();
                        postmsgobj.contents().remove();
                        postmsgobj.html(value);
                        if(flags.length > 0) flags.prependTo(postmsgobj);
                        if(fieldsetObj !== null && fieldsetObj.length > 0) fieldsetObj.appendTo(postmsgobj);
                        if(flagReasonObj !== null && flagReasonObj.length > 0) flagReasonObj.appendTo(postmsgobj);
                    }
                });
            }
        }
        if (typeof data.message == 'string' && data.message.length > 0) {
            $.alert(data.message);
        }
    }

    /**
     * 从标记的操作对话框确定,执行操作
     * @param {Object} evt
     * @param {string} txtid
     * @constructor
     */
    function RunFlag(evt, txtid) {
        txtid = (txtid ? txtid : 'flagReasonText');
        if (evt) {
            var evtobj = $(evt);
            var postdata = new Object();
            var channel_info = $('#' + txtid).val();
            var optFlag = evtobj.data('flag');
            var isfirstpost = evtobj.data('type');
            var optPostFlagCfg = CurPostFlagList;
            if(isfirstpost || !optPostFlagCfg || count(optPostFlagCfg)==0)
                optPostFlagCfg = PostFlagList;
            var urlAttach = '';
            postdata.orders = channel_info;
            if(optFlag == 'striphtml'){
                postdata.orders = '<br>';
                if(channel_info.length > 2 && channel_info[0] == '<' && channel_info[channel_info.length - 1] == '>')
                    postdata.orders = channel_info;
            }
            else{
                if(optFlag == 'clear'){
                    var isValidFlag = false;//orders是否有效:
                    if(postdata.orders.length > 0 && typeof (optPostFlagCfg) == 'object'){
                        if(typeof(optPostFlagCfg[postdata.orders]) != 'undefined'){
                            urlAttach += '-'+postdata.orders;
                            isValidFlag = true;
                        }
                    }
                    if(!isValidFlag) urlAttach += '-all';
                    postdata.orders = '';//清除
                }
            }
            if(!!IsDebug) urlAttach += "?debug=1&XDEBUG_SESSION_START=PHPSTORM";
            var opt_pid = evtobj.data('pid');
            var posturl = 'ghxpost-' + optFlag + '-' + opt_pid + urlAttach;
            postdata.isfirst = opt_pid +':'+ isfirstpost;
            $.post(posturl, postdata, function(data,status,xhr) {
                if (status == "success") {
                    $('#flagReason').modal('hide');//隐藏对话框
                    if (typeof (data) == 'string') {
                        if (data[0] == '{' && data[data.length - 1] == '}') {
                            data = JSON.parse(data);
                        }
                    }

                    if (typeof (data) == 'object' && data.code == 0 && typeof (optPostFlagCfg) == 'object') {
                        $('#flagReason #flagReasonText').val('');//清除内容
                        CatchFlagPostData(data,isfirstpost,optPostFlagCfg);
                    }
                }
            });
        }
    }

    function clearpostflag(url,sender){
        if(!!url){
            var isfirstpost = false;
            if($(sender).parents('.card-thread .message').length > 0) isfirstpost = true;
            var postdata = new Object();
            postdata.isfirst = (isfirstpost?1:0);
            $.post(url,postdata, function(rdata,status,xhr) {
                if (status == "success") {
                    if (typeof (rdata) == 'string') {
                        if (rdata[0] == '{' && rdata[rdata.length - 1] == '}') {
                            rdata = JSON.parse(rdata);
                        }
                    }
                    if (typeof (rdata) == 'object' && rdata.code == 0 && typeof (CurPostFlagList) == 'object') {
                        CatchFlagPostData(rdata,isfirstpost,PostFlagList);
                    }
                }
            });
        }
        return false;
    }
//]]>
</script>
<?php } ?>





